package com.android.aircondition.view;


import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.aircondition.R;
import com.android.aircondition.service.GreeService;

public class ModeChushiView extends RelativeLayout{
	private DropImageView drop0 ;
	private DropImageView drop1 ;
	private DropImageView drop2 ;
	private DropImageView drop3 ;
	private DropImageView drop4 ;
	private DropImageView drop5 ;
	private DropImageView drop6 ;
	private DropImageView drop7 ;
	private DropImageView drop8 ;
	private DropImageView drop9 ;
	private DropImageView drop10 ;
	private DropImageView drop11 ;
	
	private DropImageView dropLeft;
	private DropImageView dropRight;
	
	public ModeChushiView(Context context) {
		super(context,null);
		initData(context);
	}

	public ModeChushiView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initData(context);
		
	}

	private void initData(Context context){
		
		dropLeft = new DropImageView(context);
		dropRight = new DropImageView(context);
		dropLeft.setVisibility(View.INVISIBLE);
		dropRight.setVisibility(View.INVISIBLE);
		
 		drop0  = new DropImageView(context); 
 		drop1  = new DropImageView(context); 
 		drop2  = new DropImageView(context); 
 		drop3  = new DropImageView(context); 
 		drop4  = new DropImageView(context); 
 		drop5  = new DropImageView(context); 
 		drop6  = new DropImageView(context); 
 		drop7  = new DropImageView(context); 
 		drop8  = new DropImageView(context); 
 		drop9  = new DropImageView(context); 
 		drop10 = new DropImageView(context); 
 		drop11 = new DropImageView(context); 
 		
 		LayoutParams dropParamLeft   = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
 		LayoutParams dropParamRight  = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
 		LayoutParams dropParam0  = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
 		LayoutParams dropParam1  = new LayoutParams(60,66);
 		LayoutParams dropParam2  = new LayoutParams(30,32);
 		LayoutParams dropParam3  = new LayoutParams(50,56);
 		LayoutParams dropParam4  = new LayoutParams(30,32);
 		LayoutParams dropParam5  = new LayoutParams(30,32);
 		LayoutParams dropParam6  = new LayoutParams(80,87);
 		LayoutParams dropParam7  = new LayoutParams(60,66);
 		LayoutParams dropParam8  = new LayoutParams(30,32);
 		LayoutParams dropParam9  = new LayoutParams(50,56);
 		LayoutParams dropParam10 = new LayoutParams(40,48);
 		LayoutParams dropParam11 = new LayoutParams(60,66);
 		
 		dropParamLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParamLeft.setMargins(150, 100, 0, 0);
 		
 		dropParamRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
 		dropParamRight.setMargins(0, 100, 150, 0);
 		
 		dropParam0.addRule(RelativeLayout.CENTER_IN_PARENT);
 		
 		dropParam1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
 		dropParam1.setMargins(0, 140, 200, 0);
 		
 		dropParam2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
 		dropParam2.addRule(RelativeLayout.CENTER_VERTICAL);
 		dropParam2.setMargins(0, 250, 180, 0);
 		
 		dropParam3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam3.setMargins(160, 150, 0, 0);
 		
 		dropParam4.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam4.setMargins(250, 160, 0, 0);
 		
 		dropParam5.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam5.setMargins(140, 180, 0, 0);
 		
 		dropParam6.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam6.setMargins(160, 350, 0, 0);
 		
 		dropParam7.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam7.setMargins(180, 220, 0, 0);
 		
 		dropParam8.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam8.setMargins(150, 300, 0, 0);
 		
 		dropParam9.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam9.setMargins(280, 370, 0, 0);
 		
 		dropParam10.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam10.setMargins(310, 350, 0, 0);
 		
 		dropParam11.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 		dropParam11.setMargins(300, 200, 0, 0);
 		
 		
 		dropLeft.setLayoutParams(dropParamLeft);
 		dropRight.setLayoutParams(dropParamRight);
 		drop0.setLayoutParams(dropParam0);
 		drop1.setLayoutParams(dropParam1);
 		drop2.setLayoutParams(dropParam2);
 		drop3.setLayoutParams(dropParam3);
 		drop4.setLayoutParams(dropParam4);
 		drop5.setLayoutParams(dropParam5);
 		drop6.setLayoutParams(dropParam6);
 		drop7.setLayoutParams(dropParam7);
 		drop8.setLayoutParams(dropParam8);
 		drop9.setLayoutParams(dropParam9);
 		drop10.setLayoutParams(dropParam10);
 		drop11.setLayoutParams(dropParam11);
 		
 		
 		this.addView(drop0);
 		this.addView(drop1);
 		this.addView(drop2);
 		this.addView(drop3);
 		this.addView(drop4);
 		this.addView(drop5);
 		this.addView(drop6);
 		this.addView(drop7);
 		this.addView(drop8);
 		this.addView(drop9);
 		this.addView(drop10);
 		this.addView(drop11);
 		this.addView(dropLeft);
 		this.addView(dropRight);
		
	}
	
	public void startChushiAnimation(){
		AlphaAnimation animation  = new AlphaAnimation(0.0f, 1.0f);
		AlphaAnimation animation0 = new AlphaAnimation(0.0f, 1.0f);
		AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
		AlphaAnimation animation2 = new AlphaAnimation(0.0f, 1.0f);
		AlphaAnimation animation3 = new AlphaAnimation(0.0f, 1.0f);
		AnimationSet set = new AnimationSet(true);
		set.setInterpolator(new LinearInterpolator());
		
		drop0 .setAnimation(animation0);
		drop1 .setAnimation(animation0);
		drop2 .setAnimation(animation0);
		drop3 .setAnimation(animation1);
		drop4 .setAnimation(animation1);
		drop5 .setAnimation(animation1);
		drop6 .setAnimation(animation2);
		drop7 .setAnimation(animation2);
		drop8 .setAnimation(animation2);
		drop9 .setAnimation(animation3);
		drop10.setAnimation(animation3);
		drop11.setAnimation(animation3);
		
		animation .setDuration(1000);
		animation0.setDuration(1000);
		animation1.setDuration(1000);
		animation2.setDuration(1000);
		animation3.setDuration(1000);
		
		animation .setStartOffset(0);
		animation0.setStartOffset(1000);
		animation1.setStartOffset(2000);
		animation2.setStartOffset(3000);
		animation3.setStartOffset(4000);
		
		set.addAnimation(animation);
		set.addAnimation(animation0);
		set.addAnimation(animation1);
		set.addAnimation(animation2);
		set.addAnimation(animation3);
		
		set.start();
		
		DisplayMetrics dm = new DisplayMetrics();  
		dm = this.getResources().getDisplayMetrics();  
		int screenWidth = dm.widthPixels;  
		int screenHeight = dm.heightPixels;
		
		AnimationSet dropLeftSet = new AnimationSet(true);
		Animation dropLeftTransferAnimation = new TranslateAnimation(0, 0,0,screenHeight/5 );
		Animation dropLeftScaleAnimation	= new ScaleAnimation(1.0f, 1.0f, 0.9f, 2.0f);
		dropLeftTransferAnimation.setDuration(1000);
		dropLeftScaleAnimation.setDuration(1000);
		dropLeftTransferAnimation.setStartOffset(0);
		dropLeftScaleAnimation.setStartOffset(0);
		dropLeftSet.addAnimation(dropLeftTransferAnimation);
		dropLeftSet.addAnimation(dropLeftScaleAnimation);
		dropLeft.setAnimation(dropLeftSet);
		dropLeftSet.start();
		
		AnimationSet dropRightSet = new AnimationSet(true);
		Animation dropRightAlphaAnimation = new AlphaAnimation(0.0f,1.0f);
		Animation dropRightTransferAnimation = new TranslateAnimation(0, 0,0,screenHeight/5 );
		Animation dropRightScaleAnimation	= new ScaleAnimation(1.0f, 1.0f, 0.9f, 2.0f);
		dropRightTransferAnimation.setDuration(1000);
		dropRightScaleAnimation.setDuration(1000);
		dropRightTransferAnimation.setStartOffset(3000);
		dropRightScaleAnimation.setStartOffset(3000);
		dropRightAlphaAnimation.setDuration(400);
		dropRightAlphaAnimation.setStartOffset(300);
		dropRightSet.addAnimation(dropRightTransferAnimation);
		dropRightSet.addAnimation(dropRightScaleAnimation);
		dropRightSet.addAnimation(dropRightAlphaAnimation);
		dropRight.setAnimation(dropRightSet);
		dropRightSet.start();
		
		
	}
	
	private class DropImageView extends ImageView{
		
		int[] drops = {R.drawable.drop_1,R.drawable.drop_2,R.drawable.drop_3,R.drawable.drop_4,R.drawable.drop_5};

		public DropImageView(Context context) {
			super(context);
			
			int res = (int) Math.round(Math.random()*4);
			this.setBackgroundResource(drops[res]);
		}
		
	}
	
}
